<?php

class AdjustedAverage extends TeamAverage {

  function AdjustedAverage(&$team) {
    $this->team = $team;
    $this->getTeamAverages();
  }

  function hasSkill($skill, $skills) {
    return ereg("^" . strtolower($skill), strtolower($skills))
        or ereg(", ?" . strtolower($skill), strtolower($skills));
  }

  function skillCheck(&$sum, $skills, $skill, $increase) {
    if ($this->hasSkill($skill, $skills)) {
      $sum += $increase;
    }
  }

  function addMa(&$player) {
    $this->ma += $player->ma;
    $this->skillCheck($this->ma, $player->skills, "sprint", 1);
    $this->skillCheck($this->ma, $player->skills, "sure feet", 1);
    $this->skillCheck($this->ma, $player->skills, "jump up", 1);
    $this->skillCheck($this->ma, $player->skills, "dodge", 0.5);
    $this->skillCheck($this->ma, $player->skills, "break tackle", 0.5);
    $this->skillCheck($this->ma, $player->skills, "stand firm", 0.25);
  }

  function addSt(&$player) {
    $this->st += ($player->st - 3) / 2;
    $increase = ($player->st - 1) / 3;
    $this->skillCheck($this->st, $player->skills, "block", $increase * 2);
    $this->skillCheck($this->st, $player->skills, "tackle", $increase);
    $this->skillCheck($this->st, $player->skills, "guard", 0.33);
    $this->skillCheck($this->st, $player->skills, "pro", $increase);
    $this->skillCheck($this->st, $player->skills, "mighty blow", $increase * 2);
    $this->skillCheck($this->st, $player->skills, "claw", $increase * 2);
    $this->skillCheck($this->st, $player->skills, "razor sharp", $increase * 2);
    $this->skillCheck($this->st, $player->skills, "piling on", $increase * 2);
    $this->skillCheck($this->st, $player->skills, "dirty player", 0.67);
    $this->skillCheck($this->st, $player->skills, "frenzy", $increase * 2);
  }

  function addAg(&$player) {
    $ag = $player->ag;
    $this->ag += $ag;
    if ($ag > 4) {
      $ag = 4.5;
    }
    $increase = (5 - $player->ag) / 10;
    $this->skillCheck($this->ag, $player->skills, "dodge", $increase * 2);
    $this->skillCheck($this->ag, $player->skills, "pass", $increase * 2);
    $this->skillCheck($this->ag, $player->skills, "break tackle", $increase * ($player->st - 3));
    $this->skillCheck($this->ag, $player->skills, "sure hands", $increase);
    $this->skillCheck($this->ag, $player->skills, "big hand", $increase);
    $this->skillCheck($this->ag, $player->skills, "catch", $increase);
    $this->skillCheck($this->ag, $player->skills, "accurate", $increase);
    $this->skillCheck($this->ag, $player->skills, "strong arm", $increase);
    $this->skillCheck($this->ag, $player->skills, "catch", $increase);
    $this->skillCheck($this->ag, $player->skills, "leap", $increase);
    $this->skillCheck($this->st, $player->skills, "stand firm", $increase);
    $this->skillCheck($this->st, $player->skills, "nerves of steel", $increase);
  }

  function addAv(&$player) {
    $prob = getProb(1 + $player->av);

    if ($this->hasSkill("foul appearance", $player->skills)) {
      $prob = $prob * 5 / 6;
    }

    if ($this->hasSkill("thick skull", $player->skills)) {
      $prob = $prob * 15 / 16;
    }

    if ($this->hasSkill("block", $player->skills)) {
      $prob = $prob * 8 / 9;
    }

    if ($this->hasSkill("dodge", $player->skills)) {
      $prob = $prob * 8 / 9;
    }

    $this->av += $prob;
  }

  function addCost(&$player) {
    $playerCost = $player->cost;
    $position = $this->team->race->getPosition($player->position);

    // stat improvements
    $playerCost += 10 * ($player->ma - $position->ma);
    $playerCost += 30 * ($player->st - $position->st);
    $playerCost += 20 * ($player->ag - $position->ag);
    $playerCost += 10 * ($player->av - $position->av);


    // skills 20 block, 10 others
    $tok = strtok ($player->skills,",");
    while ($tok) {
      $tok = strtolower(trim($tok));
      $tok = str_replace ("+", "\\+", $tok);
      $tok = str_replace ("*", "\\*", $tok);
      if (!(strlen($position->skills) > 0 and $this->hasSkill($tok, $position->skills))) {
        if (0 == strncmp($tok, "\+", 2)) {
          // ignore stat increase
        } else if (0 == strncmp($tok, "\*", 2)) {
          // ignore comment
        } else if (0 == strcasecmp("block", $tok)) {
          $playerCost += 20;
        } else {
          $playerCost += 10;
        }
      }

      $tok = strtok (",");
    }

    // each niggle reduces cost by a sixth
    for ($i = 0; $i < $player->niggles; $i++) {
      $playerCost *= 5 / 6;
    }

    $this->addPlayerCost($playerCost);
    $this->cost += $playerCost;
  }

  function addPlayer(&$player) {
    $this->addMa($player);
    $this->addSt($player);
    $this->addAg($player);
    $this->addAv($player);
    $this->addCost($player);
  }

}
?>